package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Owner;
import com.woniuxy.service.OwnerService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.RespongseData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/owner")
@AllArgsConstructor
public class OwnerController {

    private OwnerService ownerService;

    @GetMapping("/ownerPage")
    public String userPage(){
        return "owner/owner";
    }

    @PostMapping("/queryOwner")
    @ResponseBody
    public RespongseData queryOwner(@RequestBody PageData<Owner> pageData){
        PageInfo<Owner> ownerPageInfo = ownerService.queryOwner(pageData);
        return new RespongseData(200,"ok",ownerPageInfo);
    }

    @PostMapping("/saveOwner")
    @ResponseBody
    @Transactional
    public RespongseData saveOwner(@RequestBody Owner owner){
        ownerService.saveOwner(owner);
        return new RespongseData(200,"ok",null);
    }

    @PostMapping("/batchDelOwner")
    @ResponseBody
    public  RespongseData batchDelOwner(@RequestBody List<Integer> ids){
        boolean b = ownerService.batchDelOwner(ids);
        return new RespongseData(200,"ok",b);
    }

    @GetMapping("/updateOwnerBtn/{id}")
    @ResponseBody
    public RespongseData updateOwnerBtn(@PathVariable("id") Integer id){
        Owner owner = ownerService.queruOwnerById(id);
        return new RespongseData(200,"ok",owner);
    }

    @PostMapping("/saveUpdate")
    @ResponseBody
    public RespongseData saveUpdate(@RequestBody Owner owner){
        ownerService.saveUpdate(owner);
        return new RespongseData(200,"ok",null);
    }

    @GetMapping("/saveDelOwner/{delId}")
    @ResponseBody
    public RespongseData saveDelOwner(@PathVariable("delId") Integer id){
        boolean b = ownerService.saveDelOwner(id);
        return new RespongseData(200,"ok",b);
    }

    @GetMapping("/queryDetailBtn/{id}")
    @ResponseBody
    public RespongseData queryDetailBtn(@PathVariable("id") Integer id){
        Owner owner = ownerService.queryDetailById(id);
        return new RespongseData(200,"ok",owner);
    }
}
